﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;

namespace Web.EmailTemplate
{
    public partial class EmailTemplates : System.Web.UI.Page
    {
        static List<EMAIL_TEMPLATE> emails;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            emails = getListEmailTemplate("", "", "");
            RadGrid1.DataSource = emails;            
            RadGrid1.DataBind();
        }
        private List<EMAIL_TEMPLATE> getListEmailTemplate(string templateName, string subject, string description)
        {
            List<EMAIL_TEMPLATE> emails = EmailTemplateService.GetEmailTemplateByCondition(templateName, subject, description);
            if (emails == null)
            {
                emails = new List<EMAIL_TEMPLATE>();
            }
            return emails;
        }
        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            emails = getListEmailTemplate(txtTemplateName.Text.Trim(), txtSubject.Text.Trim(), txtDescription.Text.Trim());
            RadGrid1.DataSource = emails;
            RadGrid1.DataBind();
        }

        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = emails;
        }

        protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridPagerItem)
            {
                GridPagerItem oGridPagerItem = (GridPagerItem)e.Item;
                RadComboBox oRadComboBox = (RadComboBox)oGridPagerItem.FindControl("PageSizeComboBox");
                oRadComboBox.Width = Unit.Pixel(50);
            }
        }

        protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            switch (e.CommandName)
            {
                case "Edit":
                    Response.Redirect("EditEmailTemplate.aspx?id=" + dataItem.GetDataKeyValue("ID").ToString());
                    break;
                case "Delete":
                    long emailID = Convert.ToInt64(dataItem.GetDataKeyValue("ID").ToString());
                    emails.Remove(emails.Where(x => x.ID == emailID).FirstOrDefault<EMAIL_TEMPLATE>());
                    int deleteEmail = EmailTemplateService.DeleteEmailTemplate(emailID);
                    if (deleteEmail == 1)
                        RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Delete email template successful !" + "');");
                        //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Delete email template successful !');", true);
                    else
                        RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not delete this email template !" + "');");
                        //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not delete email template !');", true);
                    break;
                default:
                    break;
            }
        }

        protected void btnAddEmailTemplate_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect("AddNewEmailTemplate.aspx");
        }
    }
}